package com.baiytfp.hf.HFDataExchange.common.utils;

import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.common.util.StringUtil;

import java.util.List;

/**
 *
 * @Description: 用于DAO层sql拼接的工具
 * @author 罗赵树
 * @date 2018年1月4日 上午10:15:28
 *
 */
public class HFPagerListSqlUtil {
	private HFPager hfPager = null;
	private List<String> params = null;
	private StringBuilder whereBuilder = null;
	private StringBuffer whereBuffer = null;

	public HFPagerListSqlUtil(HFPager hfPager, List<String> params, StringBuffer whereBuffer) {
		this.hfPager = hfPager;
		this.params = params;
		this.whereBuffer = whereBuffer;
	}

	public HFPagerListSqlUtil(HFPager hfPager, List<String> params, StringBuilder whereBuilder) {
		this.hfPager = hfPager;
		this.params = params;
		this.whereBuilder = whereBuilder;
	}

	/**
	 * 拼接sql, 从hfPager中取出该key的值, 如果不为空(null, ""), 就在where后面拼接 andWhere, 并将该hfPager该key的值添加到List<String> params中
	 * @param paramsKey 从hfPager key, 如 adjust_id
	 * @param andWhere 带有?的where, 如: "and a.adjust_id = ?"
	 * @return 该key的值是否存在
	 */
	public boolean whereAppend(String paramsKey, String andWhere) {
		return whereAppend(paramsKey, andWhere, "", "");
	}

	public boolean whereAppendLike(String paramsKey, String andWhere) {
		return whereAppend(paramsKey, andWhere, "%", "%");
	}

	public boolean whereAppend(String paramsKey, String andWhere, String params_start, String params_end) {
		if(!StringUtil.isNull(hfPager.getParam(paramsKey))) {
			if(whereBuilder != null)
				whereBuilder.append(" "+andWhere+" ");
			if(whereBuffer != null)
				whereBuffer.append(" "+andWhere+" ");
			params.add(params_start + hfPager.getParam(paramsKey) + params_end);
			return true;
		} else
			return false;
	}

	/**
	 * 打包下sql
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public String packSql(String sql) throws Exception {
		return "select * from (" + sql + ") as sel_all" + hfPager.getOrderBy();
	}

	/*
	public List<Map> getCustomerList(HFPager hfPager, Map data_right) throws Exception {
		List<String> params = new ArrayList<String>();
		String select = "";
		String from = "";
		StringBuilder where = new StringBuilder(" where 1 = 1 ");
		HFPagerListSqlUtil hfPagerListSqlUtil = new HFPagerListSqlUtil(hfPager, params, where);

		String sql = select + from + where.toString();
		List<Map> list = HFDBHelper.list_split(hfPagerListSqlUtil.packSql(sql), Map.class, hfPager, params.toArray());
		return list;
	}
	 */

}
